package cn.sccl.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.sccl.domain.Customer;
import cn.sccl.service.CustomerService;
import cn.sccl.service.impl.CustomerServiceImpl;
/**
 * @author 饶爽
 * @time 2019年8月24日 下午3:40:13
 * @version 1.0.0
 * 添加客户的Servlet
 */
public class AddCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CustomerService customerService = new CustomerServiceImpl();
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、 获得参数并封装到Customer对象
	    Customer customer = new Customer();
	    try {
            BeanUtils.populate(customer, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
	    //2、调用Service层保存客户
	    customerService.save(customer);
	    //3、 重定向到客户列表
	    response.sendRedirect(request.getContextPath()+"/listCustomer");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
